/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmConsultaCursos.java
 *
 * Created on 26/09/2009, 12:28:59
 */

package cursos.telas;

import cursos.bd.CursosDAO;
import cursos.classes.ConsultaCursosTableModel;
import cursos.classes.Cursos;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author Gabi
 */
public class FrmConsultaCursos extends javax.swing.JFrame {

    /** Creates new form FrmConsultaCursos */
    public FrmConsultaCursos() {
        try {
            cursosDAO = new CursosDAO();
            List<Cursos> listaCursos = cursosDAO.getCursos();
            model = new ConsultaCursosTableModel(listaCursos);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());

            model = new ConsultaCursosTableModel(new ArrayList<Cursos>());
        }
        initComponents();
        setLocationRelativeTo(null);
        setVisible(true);
        consultaCursojTable.setModel(model);
        consultaCursojTable.getColumnModel().getColumn(0).setPreferredWidth(200);
        consultaCursojTable.getColumnModel().getColumn(1).setPreferredWidth(90);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        consultaCursojPanel = new javax.swing.JPanel();
        botoesCursojToolBar = new javax.swing.JToolBar();
        adicionarCursojButton = new javax.swing.JButton();
        editarCursojButton = new javax.swing.JButton();
        excluirCursojButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        imprimirjButton = new javax.swing.JButton();
        voltarTelajButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        consultaCursojTable = new javax.swing.JTable();
        nomeCursojTextField = new javax.swing.JTextField();
        nomeCursojLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta de Cursos");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        URL url = this.getClass().getResource("/imagens/logo_mini.png");
        Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(imagemTitulo);
        try{
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        }catch (Exception e){
            e.printStackTrace();
        }
        consultaCursojPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        consultaCursojPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        botoesCursojToolBar.setFloatable(false);
        botoesCursojToolBar.setRollover(true);

        adicionarCursojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/save_curso.png"))); // NOI18N
        adicionarCursojButton.setText("Adicionar");
        adicionarCursojButton.setToolTipText("Adicionar Professor");
        adicionarCursojButton.setFocusable(false);
        adicionarCursojButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        adicionarCursojButton.setMaximumSize(new java.awt.Dimension(90, 80));
        adicionarCursojButton.setMinimumSize(new java.awt.Dimension(90, 80));
        adicionarCursojButton.setPreferredSize(new java.awt.Dimension(90, 80));
        adicionarCursojButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        adicionarCursojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarCursoActionPerformed(evt);
            }
        });
        botoesCursojToolBar.add(adicionarCursojButton);

        editarCursojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/edit_curso.png"))); // NOI18N
        editarCursojButton.setText("Editar");
        editarCursojButton.setToolTipText("Editar Professor");
        editarCursojButton.setEnabled(false);
        editarCursojButton.setFocusable(false);
        editarCursojButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editarCursojButton.setMaximumSize(new java.awt.Dimension(90, 80));
        editarCursojButton.setMinimumSize(new java.awt.Dimension(90, 80));
        editarCursojButton.setPreferredSize(new java.awt.Dimension(90, 80));
        editarCursojButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editarCursojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarCursoActionPerformed(evt);
            }
        });
        botoesCursojToolBar.add(editarCursojButton);

        excluirCursojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/remove_curso.png"))); // NOI18N
        excluirCursojButton.setText("Excluir");
        excluirCursojButton.setToolTipText("Excluir Professor");
        excluirCursojButton.setFocusable(false);
        excluirCursojButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        excluirCursojButton.setMaximumSize(new java.awt.Dimension(90, 80));
        excluirCursojButton.setMinimumSize(new java.awt.Dimension(90, 80));
        excluirCursojButton.setPreferredSize(new java.awt.Dimension(90, 80));
        excluirCursojButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        excluirCursojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirCursoActionPerformed(evt);
            }
        });
        botoesCursojToolBar.add(excluirCursojButton);
        botoesCursojToolBar.add(jSeparator1);

        imprimirjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/impressao.png"))); // NOI18N
        imprimirjButton.setText("Imprimir");
        imprimirjButton.setToolTipText("Imprimir");
        imprimirjButton.setFocusable(false);
        imprimirjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        imprimirjButton.setMaximumSize(new java.awt.Dimension(90, 80));
        imprimirjButton.setMinimumSize(new java.awt.Dimension(90, 80));
        imprimirjButton.setPreferredSize(new java.awt.Dimension(90, 80));
        imprimirjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        imprimirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imprimirActionPerformed(evt);
            }
        });
        botoesCursojToolBar.add(imprimirjButton);

        voltarTelajButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/voltar.png"))); // NOI18N
        voltarTelajButton.setText("Voltar");
        voltarTelajButton.setToolTipText("Voltar");
        voltarTelajButton.setFocusable(false);
        voltarTelajButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        voltarTelajButton.setMaximumSize(new java.awt.Dimension(90, 80));
        voltarTelajButton.setMinimumSize(new java.awt.Dimension(90, 80));
        voltarTelajButton.setPreferredSize(new java.awt.Dimension(90, 80));
        voltarTelajButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        voltarTelajButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                voltarTelaActionPerformed(evt);
            }
        });
        botoesCursojToolBar.add(voltarTelajButton);

        consultaCursojPanel.add(botoesCursojToolBar, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, 470, -1));

        consultaCursojTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"", null, null},
                {"", null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Curso", "Inicio", "Término"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        consultaCursojTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        consultaCursojTable.getTableHeader().setReorderingAllowed(false);
        consultaCursojTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                consultaCursojTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(consultaCursojTable);
        consultaCursojTable.getColumnModel().getColumn(0).setMinWidth(300);
        consultaCursojTable.getColumnModel().getColumn(0).setPreferredWidth(300);
        consultaCursojTable.getColumnModel().getColumn(0).setMaxWidth(300);

        consultaCursojPanel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 460, 170));

        nomeCursojTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nomeCursojTextFieldKeyPressed(evt);
            }
        });
        consultaCursojPanel.add(nomeCursojTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 460, -1));

        nomeCursojLabel.setText("Nome do Curso");
        consultaCursojPanel.add(nomeCursojLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 23, -1, -1));

        getContentPane().add(consultaCursojPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 500, 370));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void adicionarCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarCursoActionPerformed
        new FrmCadastroCursos(new Cursos());
}//GEN-LAST:event_adicionarCursoActionPerformed
    private void voltarTelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_voltarTelaActionPerformed
        this.dispose();
}//GEN-LAST:event_voltarTelaActionPerformed
    private void editarCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarCursoActionPerformed
        int linha = consultaCursojTable.getSelectedRow();
        if (linha >= 0 ){
            Cursos curso = model.listaCursos.get(linha);
            new FrmCadastroCursos(curso);
        }
    }//GEN-LAST:event_editarCursoActionPerformed
    private void excluirCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirCursoActionPerformed
        int linha = consultaCursojTable.getSelectedRow();
        if (linha >= 0 ){
            Cursos curso = model.listaCursos.get(linha);
        try{
        cursosDAO.excluir(curso);
        JOptionPane.showMessageDialog(this,"Dados excluidos com sucesso");
        }catch (Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this,"Dados não foram excluidos");
        }
        }
    }//GEN-LAST:event_excluirCursoActionPerformed
    private void imprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imprimirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_imprimirActionPerformed
    private void consultaCursojTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_consultaCursojTableMouseClicked
        if (consultaCursojTable.getSelectedRow()>=0){
            editarCursojButton.setEnabled(true);
        }else if (evt.getClickCount() == 2){
            int linha = consultaCursojTable.getSelectedRow();
        if (linha >= 0 ){
            Cursos curso = model.listaCursos.get(linha);
            new FrmCadastroCursos(curso);
        }
        }
    }//GEN-LAST:event_consultaCursojTableMouseClicked

    private void nomeCursojTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nomeCursojTextFieldKeyPressed
        String curso = nomeCursojTextField.getText();
        List listaCursos = cursosDAO.pesquisaCurso(curso);
        model.listaCursos = listaCursos;
        model.fireTableDataChanged();
    }//GEN-LAST:event_nomeCursojTextFieldKeyPressed
    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton adicionarCursojButton;
    private javax.swing.JToolBar botoesCursojToolBar;
    private javax.swing.JPanel consultaCursojPanel;
    private javax.swing.JTable consultaCursojTable;
    private javax.swing.JButton editarCursojButton;
    private javax.swing.JButton excluirCursojButton;
    private javax.swing.JButton imprimirjButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JLabel nomeCursojLabel;
    private javax.swing.JTextField nomeCursojTextField;
    private javax.swing.JButton voltarTelajButton;
    // End of variables declaration//GEN-END:variables
    private CursosDAO cursosDAO;
    private ConsultaCursosTableModel model;
}
